package js.web.center.netty.thread;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import io.netty.channel.ChannelHandlerContext;
import js.web.center.netty.net.JsWebNetApi;

public class JsWebSocketUpdateThread implements Runnable {
	private Logger logger=Logger.getLogger(JsWebSocketUpdateThread.class);
	private long outTime=60000;
	private long sleepTime=30000;
	@Override
	public void run() {
		while(true){
			try{
				HashMap<ChannelHandlerContext,Long> allHandlers = JsWebNetApi.getAllHandlers();
				Iterator<Entry<ChannelHandlerContext, Long>> iterator = allHandlers.entrySet().iterator();
				while(iterator.hasNext()){
					Entry<ChannelHandlerContext, Long> next = iterator.next();
					if(System.currentTimeMillis()-next.getValue()>outTime){
						JsWebNetApi.removeChannelHandler(next.getKey());
					}
				}
			}catch(Exception e){
				logger.error("JsWebSocketUpdateThread ERROR:{}",e);
			}
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException e) {
				logger.error("JsWebSocketUpdateThread ERROR:{}",e);
			}
		}
	}
}
